package es.jclm.agri.foragri.domain;

import java.io.IOException;

import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.application.NavigationHandler;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.stereotype.Component;
  
@Component
public class TreeBean {  
      
    private TreeNode root; 
    
    private TreeNode selectedNode; 
  
    public TreeBean() {  
        root = new DefaultTreeNode("Root", null);  
        TreeNode node_0 = new DefaultTreeNode("Lista Empleados", root);  
        
        TreeNode node0 = new DefaultTreeNode("Node 0", root);  
        TreeNode node1 = new DefaultTreeNode("Node 1", root);  
        TreeNode node2 = new DefaultTreeNode("Node 2", root);  
          
        TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);  
        TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);  
          
        TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);  
        TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);  
          
        TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);  
        TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);  
        TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);  
          
        TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);  
    }  
  
    public TreeNode getRoot() {  
        return root;  
    } 
    
    public TreeNode getSelectedNode() {  
        return selectedNode;  
    }  
  
    public void setSelectedNode(TreeNode selectedNode) {  
        this.selectedNode = selectedNode;  
    }  
    
    
    public void onNodeSelect(NodeSelectEvent event) {  
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().getData().toString());  
        FacesContext.getCurrentInstance().addMessage(null, msg); 
        // redirect here
        FacesContext facesContext = FacesContext.getCurrentInstance();
        String redirect = "listaempleados";
        System.out.println("redirect="+redirect);
        NavigationHandler myNav = facesContext.getApplication().getNavigationHandler();
        myNav.handleNavigation(facesContext, "listaempleados", redirect);
/*
        FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = ctx.getExternalContext();
        String newUrl = ectx.encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, "/listadoempleados"));
       
        String pagePath="listaempleados.xhtml";
       
        
        try {
        //	 String path = ectx.getRequestContextPath();
        //	ectx.redirect(path + pagePath);
        //    ctx.responseComplete();
            ectx.redirect(newUrl);
            ctx.responseComplete();
        } catch (IOException ex) {
            throw new FacesException(ex);
        }
        */
    }
    public void displaySelectedSingle(ActionEvent event) {  
        if(selectedNode != null) {  
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());  
  
            FacesContext.getCurrentInstance().addMessage(null, message);  
        }  
    }  
    
}
